<?php

/*
 * Copyright (c) 2012 Aalto University and University of Helsinki
 * MIT License
 * see LICENSE.txt for more information
 */

class ConceptDAO {

  private $label;
  private $lang;
  private $uri;
  private $vocab;
  private $properties;
  private $foundby;
  private $conceptType;
  private $foreignLabels;
  private $exvocab;

  public function __construct() {
    $this->properties = array();
    $this->foreignLabels = array();
  }

  /**
   * Sets the values for the most important properties.
   * @param string $label
   * @param string $vocab
   * @param string $uri
   * @param string $lang
   */
  public function initialize($label, $vocab, $uri, $lang) {
    $this->label = $label;
    $this->vocab = $vocab;
    $this->uri = $uri;
    $this->lang = $lang;
    $this->setType();
  }

  public function setForeignLabel($label, $lang) {
    $this->foreignLabels[$lang] = $label;
  }

  public function getForeignLabels() {
    return $this->foreignLabels;
  }

  public function setLabel($label) {
    $this->label = $label;
  }

  public function getLabel() {
    return $this->label;
  }

  public function setVocab($vocab) {
    $this->vocab = $vocab;
  }

  public function getVocab() {
    return $this->vocab;
  }

  public function setURI($uri) {
    $this->uri = $uri;
  }

  public function getURI() {
    return $this->uri;
  }

  public function setFoundBy($altlabel) {
    $this->foundby = $altlabel;
  }

  public function getFoundBy() {
    return $this->foundby;
  }

  public function setType($type = 'Concept') {
    $this->conceptType = $type;
  }

  public function getType() {
    return $this->conceptType;
  }

  public function addProperty($proplabel, $uri, $vocab, $lang = null, $label = null, $exvocab = null) {
    $newProp = new propertyDAO($proplabel, $label, $uri, $vocab, $lang, $exvocab);
    $this->properties[$proplabel][$label] = $newProp;
    return $newProp;
  }
  
  public function sortProperties($property) {
    uksort($this->properties[$property], 'strcasecmp');
//    var_dump($this->properties[$property]);
  }

  public function setProperties($array) {
    $this->addProperty($array['prop'], $array['concept_uri'], $array['vocab'], $array['lang'], $array['label'], $array['exvocab']);
  }

  public function mergeNarrowerProperties($array) {
    if (isset($this->properties['skos:narrower']))
      $this->properties['skos:narrower'] = array_merge($array, $this->properties['skos:narrower']);
  }

  public function deleteProperty($property, $value = null) {
    if ($value == null) {
      if (isset($this->properties[$property])) {
        unset($this->properties[$property]);
      }
      return;
    }
    foreach ($this->properties[$property] as $key => $prop)
      if ($prop->getValue() === $value)
        unset($this->properties[$property][$key]);
  }

  public function setSortedProperties($array) {
    $this->properties = $array;
    $this->cleanUp();
  }

  public function getProperties() {
    return $this->properties;
  }

  public function getProperty($property, $uri = null) {
    if ($uri != null && !empty($this->properties)) {
      foreach ($this->properties as $prop)
        if ($prop->getUri() === $uri)
          return $prop;
    }
    else if (isset($this->properties[$property]))
      return $this->properties[$property];
    return null;
  }

  public function getPropertyUri($property) {
    if (isset($this->properties[$property]))
      foreach ($this->properties[$property] as $prop)
        return $prop->getUri();
    return null;
  }

  public function setLang($lang) {
    $this->lang = $lang;
  }

  public function getLang() {
    return $this->lang;
  }

  public function hasProperty($property) {
    return key_exists($property, $this->properties);
  }

  /**
   * Hides unwanted properties and sets the concept type properly.
   */
  private function cleanUp() {
    /* checking if unwanted skos transitives are found and if so removing them from the view */
    $this->deleteProperty('skos:broaderTransitive');
    $this->deleteProperty('skos:narrowerTransitive');

    /* checking if more specific skosext properties are found and if so removing the redundant skos properties from the view */
    if ($this->hasProperty('skosext:broaderGeneric') || $this->hasProperty('skosext:broaderPartitive')) {
      $this->deleteProperty('skos:broader');
    }
    if ($this->hasProperty('skosext:narrowerGeneric') || $this->hasProperty('skosext:narrowerPartitive'))
      $this->deleteProperty('skos:narrower');
    /* checking for and removing unwanted properties from the users view */
    $this->deleteProperty('skos:prefLabel');
    $this->deleteProperty('skos:hiddenLabel');
    if ($this->getPropertyUri('rdf:type') != null)
      $this->setType($this->getPropertyUri('rdf:type'));
    $this->deleteProperty('rdf:type');
    $this->deleteProperty('skos:inScheme');
    $this->deleteProperty('owl:sameAs');
    $this->deleteProperty('skos:topConceptOf');
  }

}

class propertyDAO {

  private $lang;
  private $exvocab;
  private $type;
  private $value;
  private $uri;
  private $vocab;
  private $submembers;

  public function __construct($type, $value, $uri, $vocab, $lang, $exvocab = null) {
    $this->submembers = array();
    $this->lang = $lang;
    $this->exvocab = $exvocab;
    $this->type = $type;
    $this->value = $value;
    $this->uri = $uri;
    $this->vocab = $vocab;
  }

  public function setLang($param) {
    $this->lang = $param;
  }

  public function getLang() {
    return $this->lang;
  }

  public function setExVocab($param) {
    $this->exvocab = $param;
  }

  public function getExVocab() {
    return $this->exvocab;
  }

  public function setType($param) {
    $this->type = $param;
  }

  public function getType() {
    return $this->type;
  }

  public function setValue($param) {
    $this->value = $param;
  }

  public function getValue() {
    return $this->value;
  }

  public function setUri($param) {
    $this->uri = $param;
  }

  public function getUri() {
    return $this->uri;
  }

  public function setVocab($param) {
    $this->vocab = $param;
  }

  public function getVocab() {
    return $this->vocab;
  }

  public function addSubMember($type, $value, $uri, $vocab, $lang, $exvocab = null) {
    $this->submembers[$value] = new propertyDAO($type, $value, $uri, $vocab, $lang, $exvocab = null);
    $this->sortSubMembers();
  }

  public function getSubMembers() {
    if (empty($this->submembers))
      return null;
    return $this->submembers;
  }

  public function sortSubMembers() {
    if (!empty($this->submembers))
      ksort($this->submembers);
  }

}

?>